local skel = fk.CreateSkill {
  name = "lb__guihuan",
  tags = {Skill.Compulsory}
}
local U = require "packages.utility.utility"
local LB_U = require "packages.lb_kill_utility.utility"

Fk:loadTranslationTable {
  ["lb__guihuan"] = "诡幻",
  [":lb__guihuan"] = "<b>锁定技</b>，当你体力值变化后，你选择上次未选择的一项：1.隐匿，此次隐匿不因此次体力变化登场，你下次登场时视为使用一张普通锦囊牌；2.减1点体力上限，摸等同于你发动本技能次数的牌。",

  ["@lb__guihuan"] = "诡幻",

  ["#lb__guihuan_1"] = "隐匿，你下次登场时视为使用一张普通锦囊牌",
  ["#lb__guihuan_2"] = "减1点体力上限，摸%arg张牌",

  ["#lb__guihuan_yinni"] = "隐匿",
  ["#lb__guihuan_draw"] = "摸牌",

  ["#lb__guihuan"] = "诡幻：请视为使用一张普通锦囊牌",

  ["$lb__guihuan1"] = "灭顶之灾。",
  ["$lb__guihuan2"] = "玄根之力。",
  ["$lb__guihuan3"] = "如此顽抗，可笑。",
  ["$lb__guihuan4"] = "是时候，结束这一切了！",
}

skel:addEffect(fk.HpChanged, {
  anim_type = "defensive",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player:getMark("__hidden_record") == 0 -- 因隐匿变化体力不发动
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local all = {"#lb__guihuan_1", "#lb__guihuan_2:::"..player:usedSkillTimes(skel.name, Player.HistoryGame) + 1}
    local choice = player:getMark("lb__guihuan")
    if choice == 0 then
      choice = room:askToChoice(player, {
        choices = all,
        skill_name = skel.name,
      })
      choice = choice == "#lb__guihuan_1" and "#lb__guihuan_yinni" or "#lb__guihuan_draw"
    end
    event:setCostData(self, {choice = choice})
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self).choice
    local mark = choice == "#lb__guihuan_draw" and "#lb__guihuan_yinni" or "#lb__guihuan_draw"  -- 反过来
    room:setPlayerMark(player, "lb__guihuan", mark)
    room:setPlayerMark(player, "@lb__guihuan", mark)
    if choice == "#lb__guihuan_yinni" then
      room:notifySkillInvoked(player, skel.name, "defensive")
      player:broadcastSkillInvoke(skel.name, math.random(1, 2))
      room:setPlayerMark(player, "lb__guihuan_yinni", 1)
      data.extra_data = data.extra_data or {}
      data.extra_data.lb__guihuan = player.id
      LB_U.enterHidden(player, "lb__huanlongyinni")
    else
      room:notifySkillInvoked(player, skel.name, "drawcard")
      player:broadcastSkillInvoke(skel.name, math.random(1, 2) + 2)
      room:changeMaxHp(player, -1)
      if player:isAlive() then
        player:drawCards(player:usedSkillTimes(skel.name, Player.HistoryGame), skel.name)
      end
    end
  end,
})

skel:addEffect(U.GeneralAppeared, {
  priority = 2,
  anim_type = "offensive",
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name, true) and target == player and player:getMark("lb__guihuan_yinni") > 0 then
      player.room:setPlayerMark(player, "lb__guihuan_yinni", 0)
      return player:hasSkill(skel.name)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local use = room:askToUseVirtualCard(player, {
      name = Fk:getAllCardNames("t"),
      skill_name = skel.name,
      prompt = "#lb__guihuan",
      cancelable = false,
      skip = true,
    })
    if use then
      event:setCostData(self, {use = use})
      return use
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local use = event:getCostData(self).use ---@type UseCardDataSpec
    room:useCard(use)
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  if player:getMark("lb__guihuan") ~= 0 then
    player.room:setPlayerMark(player, "@lb__guihuan", player:getMark("lb__guihuan"))
  end
end)

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@lb__guihuan", 0)
end)

return skel